package com.share.finance.asset.sjd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.share.common.common.Page;
import com.share.finance.asset.sjd.mapper.UserInfoMapper;
import com.share.finance.asset.sjd.model.db.UserBindCard;
import com.share.finance.asset.sjd.model.db.UserFile;
import com.share.finance.asset.sjd.model.db.UserInfo;
import com.share.finance.asset.sjd.model.vo.UserPageReq;
import com.share.finance.asset.sjd.service.IUserBindCardService;
import com.share.finance.asset.sjd.service.IUserFileService;
import com.share.finance.asset.sjd.service.IUserInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户基本信息  服务实现类
 * </p>
 *
 * @author author
 * @since 2023-09-08
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {
    @Resource
    private UserInfoMapper userInfoMapper;

    @Autowired
    private IUserFileService userFileService;

    @Autowired
    private IUserBindCardService userBindCardService;

    @Override
    public Page pageList(UserPageReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(req.getUserId())) {
            queryWrapper.like(UserInfo::getUserId, req.getUserId());
        }
        if (StringUtils.isNotBlank(req.getUserName())) {
            queryWrapper.like(UserInfo::getUserName, req.getUserName());
        }
        if (StringUtils.isNotBlank(req.getPhoneNo())) {
            queryWrapper.like(UserInfo::getPhoneNo, req.getPhoneNo());
        }
        if (StringUtils.isNotBlank(req.getIdCardNo())) {
            queryWrapper.like(UserInfo::getIdCardNo, req.getIdCardNo());
        }
        if (StringUtils.isNotBlank(req.getIdCardAddress())) {
            queryWrapper.like(UserInfo::getIdCardAddress, req.getIdCardAddress());
        }
        if (StringUtils.isNotBlank(req.getSex())) {
            queryWrapper.like(UserInfo::getSex, req.getSex());
        }
        if (StringUtils.isNotBlank(req.getProvince())) {
            queryWrapper.like(UserInfo::getProvince, req.getProvince());
        }
        if (StringUtils.isNotBlank(req.getNation())) {
            queryWrapper.like(UserInfo::getNation, req.getNation());
        }
        queryWrapper.orderByDesc(UserInfo::getId);
        List<UserInfo> list = userInfoMapper.selectList(queryWrapper);
        Page<UserInfo> page = new Page<>(list, new PageInfo<>(list).getTotal(), req);

        if (!CollectionUtils.isEmpty(list)) {
            List<String> userIdList = list.stream().map(e -> e.getUserId()).collect(Collectors.toList());
            Map<String, List<UserFile>> userFileMap = userFileService.listByUserIds(userIdList, null);
            Map<String, List<UserBindCard>> userBindCardMap = userBindCardService.listByUserIds(userIdList, null);
            for (UserInfo user : list) {
                if (!CollectionUtils.isEmpty(userFileMap)) {
                    user.setUserFiles(userFileMap.get(user.getUserId()));
                }

                if (!CollectionUtils.isEmpty(userBindCardMap)) {
                    user.setUserBindCards(userBindCardMap.get(user.getUserId()));
                }
            }
        }
        return page;
    }

    @Override
    public UserInfo getByIdCard(String idCard) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getIdCardNo, idCard);
        queryWrapper.eq(UserInfo::getUserStatus, 1);
        queryWrapper.orderByDesc(UserInfo::getId);
        queryWrapper.last("limit 1");
        return userInfoMapper.selectOne(queryWrapper);
    }
}
